# QAQ
# 主程序，新坑

try:
    import time
    import playsound
    from tkinter import *
    from tkinter.ttk import *
    from ttkbootstrap import *
    from PIL import Image, ImageTk
    import ctypes
    import webbrowser

    from Logging import *
    from Download_using import *
    from Launch_playing import *
    from Local_files import *
except:
    logger.Logger.error("警告！启动器启动失败，出现严重错误：请将此错误提交给我们！")
    exit(6)

logger.Logger.info("主进程已正式开始。")

top_version_name_level = Toplevel()
top_version_name_level.destroy()
top_showed_level = Toplevel()
top_showed_level.destroy()

mark = False
marked = False

# 加载一堆奇怪的图片
icon_release__ = Image.open(fp=config.release_tag_path, mode="r")
icon_release_ = icon_release__.resize((30, 30))
icon_release = ImageTk.PhotoImage(icon_release_)
icon_release__.close()
icon_snapshot__ = Image.open(fp=config.snapshot_tag_path, mode="r")
icon_snapshot_ = icon_snapshot__.resize((30, 30))
icon_snapshot = ImageTk.PhotoImage(icon_snapshot_)
icon_snapshot__.close()
icon_cute_fish__ = Image.open(fp=config.cute_fish_path, mode="r")
icon_cute_fish_ = icon_cute_fish__.resize((77, 49))
icon_cute_fish = ImageTk.PhotoImage(icon_cute_fish_)
icon_cute_fish__.close()
icon_baby_husk_targeting__ = Image.open(fp=config.baby_husk_targeting_path, mode="r")
icon_baby_husk_targeting_ = icon_baby_husk_targeting__.resize((70, 130))
icon_baby_husk_targeting = ImageTk.PhotoImage(icon_baby_husk_targeting_)


# 需要的部分函数
class main_func:

    def get_list_tuple(self: int, list_: list, *prefix_: str):
        list__: list = []
        for i in list_:
            if "name" in i:
                num: str = str(i["name"])
            else:
                num: str = str(i["version"])
            if prefix_:
                prefix = prefix_[0]
                list__.append(prefix + num)
            else:
                list__.append(num)
        list_tuple = tuple(list__)
        return list_tuple

    def get_entry(self: int):
        player_name = player_name_entry.get()
        if not player_name:
            logger.Logger.warning("无玩家名，无法启动")
            return None
        game_version = game_version_choose.get()
        if not game_version:
            logger.Logger.warning("未选择游戏版本，无法启动。")
            return None
        java_version_ = java_version_choose.get()
        if not java_version_:
            logger.Logger.warning("未选择Java版本，无法启动。")
            return None
        java_version = java_version_.split(" ")[1]
        max_memory_ = max_memory_entry.get()
        if not max_memory_:
            logger.Logger.warning("未设置最大内存，无法启动。")
            return None
        max_memory = "-Xmx" + max_memory_ + "M"
        min_memory_ = min_memory_entry.get()
        if not min_memory_:
            logger.Logger.warning("未设置最小内存，无法启动。")
            return None
        min_memory = "-Xmn" + min_memory_ + "M"
        player_uuid = player_uuid_entry.get()
        if not player_uuid:
            logger.Logger.warning("未设置UUID，无法启动。")
        launch_info = (java_version, game_version, max_memory, min_memory, player_name, player_uuid)
        logger.Logger.info(launch_info)
        launcher_playing_vanilla(launch_info)
        return None

    def print_thread_count(self: int):
        n = 0
        while n == 0:
            logger.Logger.debug("当前活跃线程数：" + str(threading.active_count()))
            logger.Logger.debug("分别为：" + str(threading.enumerate()))
            time.sleep(10)

    def bottom_progressbar_update(self: int):
        global mark, marked
        while self:
            bottom_progress.set(threading.active_count())
            time.sleep(0.5)
            marked = bit_version_picture()
            if mark != marked:
                picture_ = Image.open(fp=marked)
                picture_ = picture_.resize((360, 200))
                feature_picture_ = ImageTk.PhotoImage(picture_)
                feature_label.config(image=feature_picture_)
                feature_label.update()
                mark = marked
            version_temp_ = ".".join(func.get_version_info(0, game_version_choose.get())[0].split(".")[0:2])
            temp_1_, temp_2_ = local_files.read_features(0, version_temp_)
            version_feature_topic.set("更新主题   " + temp_1_)
            version_feature_introduction.set("      " + temp_2_)
        return None


def showinfo(title: str, text: str, image: str):
    """
    用以产生一个消息风格的消息框。
    :param title: 消息框标题，接在“[信息]”字样之后。
    :param text: 消息内容。
    :param image: 指定调用的一张图片，需要类型为PhotoImage。
    :return: 此函数不返回值。
    """
    global top_showed_level
    logger.Logger.info("正在创建信息窗口……")
    try:
        top_showed_level.destroy()
        logger.Logger.debug("已销毁存在的顶层子窗口。")
    except:
        pass
    top_showed_level = Toplevel()
    top_showed_level.resizable(False, False)
    top_showed_level.title(config.tk_lang_info_title + " " + title)
    info_frame = Labelframe(top_showed_level, text=config.tk_lang_info_title_, style="info")
    info_frame.pack()
    Label(info_frame, image=image, text=text, compound="left").pack()
    Label(info_frame, text=config.tk_lang_close_box_tip).pack()
    top_showed_level.update()
    logger.Logger.debug("信息窗口创建完成。")


def showwarning(title: str, text: str, image: str):
    """
    用以产生一个警告风格的消息框。
    :param title: 消息框标题，接在“[警告]”字样之后。
    :param text: 消息内容。
    :param image: 指定调用的一张图片，需要类型为PhotoImage。
    :return: 此函数不返回值。
    """
    global top_showed_level
    logger.Logger.info("正在创建警告窗口……")
    try:
        top_showed_level.destroy()
        logger.Logger.debug("已销毁存在的顶层子窗口。")
    except:
        pass
    top_showed_level = Toplevel()
    top_showed_level.resizable(False, False)
    top_showed_level.title(config.tk_lang_warning_title + " " + title)
    warning_frame = Labelframe(top_showed_level, text=config.tk_lang_warning_title_, style="warning")
    warning_frame.pack()
    Label(warning_frame, image=image, text=text, compound="left").pack()
    Label(warning_frame, text=config.tk_lang_close_box_tip).pack()
    top_showed_level.update()
    logger.Logger.debug("警告窗口创建完成。")


def showerror(title: str, text: str, image: str):
    """
    用以产生一个错误风格的消息框。
    :param title: 消息框标题，接在“[错误]”字样之后。
    :param text: 消息内容。
    :param image: 指定调用的一张图片，需要类型为PhotoImage。
    :return: 此函数不返回值。
    """
    global top_showed_level
    logger.Logger.info("正在创建错误窗口……")
    try:
        top_showed_level.destroy()
        logger.Logger.debug("销毁存在的顶层子窗口。")
    except:
        pass
    top_showed_level = Toplevel()
    top_showed_level.resizable(False, False)
    top_showed_level.title(config.tk_lang_error_title + " " + title)
    error_frame = Labelframe(top_showed_level, text=config.tk_lang_error_title_, style="danger")
    error_frame.pack()
    Label(error_frame, image=image, text=text, compound="left").pack()
    Label(error_frame, text=config.tk_lang_close_box_tip).pack()
    top_showed_level.update()
    logger.Logger.debug("错误窗口创建完成。")


def bit_version_picture():
    game_version_name = game_version_choose.get()
    game_version_dict_list = local_files.game_versions_list(0)
    for i in game_version_dict_list:
        if i["name"] == game_version_name:
            Minecraft_version = str(i["version"])
            temp_1 = Minecraft_version.split(".")
            temp_2 = temp_1[0: 2]
            temp_3 = ".".join(temp_2)
            picture_path = config.picture_path + "Minecraft." + temp_3 + ".png"
            return picture_path

    picture_path = config.picture_path + "Minecraft.1.18.png"
    return picture_path


def get_launch():
    T = threading.Thread(target=main_func.get_entry, args=(0,), daemon=False)
    T.start()
    logger.Logger.info("启动命令已经执行，希望游戏顺利启动。")


def get_print_thread_count():
    T = threading.Thread(target=main_func.print_thread_count, args=(0,), daemon=True, name="Threads_printer")
    T.start()
    logger.Logger.debug("已创建子进程，监控活跃进程数。")


def music_player():
    playsound.playsound("FCL\\music.mp3")
    logger.Logger.info("音乐播放完毕。")


def background_music():
    logger.Logger.info("尝试播放音乐……")
    try:
        threading.Thread(target=music_player, daemon=True, name="Music_player").start()
        logger.Logger.debug("已创建子线程，播放背景音乐music.mp3。")
    except:
        logger.Logger.warning("播放音乐失败，请检查FCL目录内是否有文件“music.mp3”，或检查该文件的可用性。")


def bottom_progressbar_():
    T = threading.Thread(target=main_func.bottom_progressbar_update, args=(5,), daemon=True, name="Thread_player")
    T.start()
    logger.Logger.debug("已创建子线程，更新GUI第一页的线程数进度条。")


def search_version_result():
    """
    获取输入的信息，从而查询指定版本。
    :return: 查询成功，返回该版本的[0]版本号、[1]JSONurl、[2]版本类型、[3]最近更改日期、[4]正式发布日期，以元组形式返回。查询失败，返回None。
    """
    version_info_package = search_new_version(search_a_version_entry.get(), download_api_choose.get())
    if version_info_package:
        version_num__.set(version_info_package[0])
        version_type__.set(version_info_package[2])
        version_update_date__.set(version_info_package[3])
        version_release_date__.set(version_info_package[4])
        version_json_url__.set("                          " + config.tk_lang_get_version_json_true)
        logger.Logger.info("查询完毕，使用" + download_api_choose.get() + "下载源时，该版本可以下载：" + search_a_version_entry.get())
    else:
        version_num__.set("")
        version_type__.set("")
        version_update_date__.set("")
        version_release_date__.set("")
        version_json_url__.set("                          " + config.tk_lang_get_version_json_false)
        logger.Logger.warning("查询完毕，使用" + download_api_choose.get() + "下载源时，该版本无法下载：" + search_a_version_entry.get())
    return version_info_package


def get_download_whole_version(version: str, name: str, mode: str):
    logger.Logger.debug("点击了确认下载按钮，开始响应。")
    func.threading_manager.acquire()
    threading.Thread(target=download_whole_version, args=(version, name, mode),
                     name="Download_threading", daemon=True).start()
    func.threading_manager.release()
    try:
        top_version_name_level.destroy()
    except:
        pass


def get_version_update_info(version_: str, type_: str):

    if not version_:
        logger.Logger.warning("似乎未选择版本，无法展示详情。")
        return None

    logger.Logger.debug("点击了展示详情的按钮，开始拼接版本wiki页url。")
    url_prefix = "https://wiki.biligame.com/mc/"
    if type_ == "正式版":
        url = url_prefix + "Java版" + version_
    elif type_ == "快照版":
        url = url_prefix + version_
    else:
        logger.Logger.error("拼接版本wiki页的url时发生错误，无法显示。")
        showerror("打开wiki失败", "拼接wiki页的url时失败，该版本类型似乎暂不支持直接打开wiki页……", icon_cute_fish)
        return None
    logger.Logger.debug("成功获取wiki页的链接，正在打开……")
    webbrowser.open_new(url=url)
    logger.Logger.info("已打开指定wiki页。")
    return None


def download_version_result(version: str, mode: str):
    logger.Logger.info("已创建一个子线程，准备展示窗口。")
    global top_version_name_level
    try:
        top_version_name_level.destroy()
        logger.Logger.warning("发现一个已展示的信息窗口，已将其关闭。")
    finally:
        logger.Logger.debug("创建中……")
        top_version_name_level = Toplevel()
        top_version_name_level.title(config.tk_lang_toplevel_title_version_name)
        top_version_name_level.resizable(False, False)
        logger.Logger.debug("窗口基本信息创建完毕。")

    Label(top_version_name_level, text=config.tk_lang_toplevel_title_version_name_message, font=config.font_cof) \
        .grid(row=0, column=0,
              columnspan=3)

    top_frame_info = Labelframe(top_version_name_level, text=config.tk_lang_toplevel_version_title_info_frame,
                                style="warning")
    top_frame_info.grid(row=1, column=0, rowspan=2)
    Label(top_frame_info, text=config.tk_lang_toplevel_info_tip_1, font=config.font_cof).grid(row=0, column=0)
    Label(top_frame_info, text=config.tk_lang_toplevel_info_tip_2, font=config.font_cof).grid(row=1, column=0)
    Label(top_frame_info, text=config.tk_lang_toplevel_info_tip_3, font=config.font_cof).grid(row=2, column=0)
    Label(top_frame_info, text=config.tk_lang_toplevel_info_tip_4, font=config.font_cof).grid(row=3, column=0)
    Label(top_frame_info, text=config.tk_lang_toplevel_info_tip_5, font=config.font_cof).grid(row=4, column=0)

    info_package = search_version_result()

    Label(top_frame_info, text=info_package[0], font=config.font_cof).grid(row=0, column=1)
    Label(top_frame_info, text=info_package[2], font=config.font_cof).grid(row=1, column=1)
    Label(top_frame_info, text=info_package[3], font=config.font_cof).grid(row=2, column=1)
    Label(top_frame_info, text=info_package[4], font=config.font_cof).grid(row=3, column=1)
    Label(top_frame_info, text="普通安装，不进行版本隔离。", font=config.font_cof).grid(row=4, column=1)

    varname = StringVar()

    top_frame_setting = Labelframe(top_version_name_level, text=config.tk_lang_toplevel_version_title_setting_frame,
                                   style="primary")
    top_frame_setting.grid(row=1, column=1, columnspan=2)
    Label(top_frame_setting, text=config.tk_lang_toplevel_setting_tip_1, font=config.font_cof).grid(row=0, column=0)
    name_entry = Entry(top_frame_setting, textvariable=varname, style="primary", font=config.font_cof)
    name_entry.grid(row=0, column=1)

    temp_1 = info_package[0]
    varname.set(temp_1)
    logger.Logger.debug("两个信息模块创建完毕。")

    Button(top_version_name_level, text=config.tk_lang_toplevel_confrim_button, style="info",
           command=lambda: get_download_whole_version(version, name_entry.get(), mode)).grid(row=2, column=1)
    Button(top_version_name_level, text=config.tk_lang_version_info_button, style="warning",
           command=lambda: get_version_update_info(info_package[0], info_package[2])).grid(row=2, column=2)

    logger.Logger.debug("附属窗口已弹出。")

    def saved_and_quit_toplevel():
        logger.Logger.debug("附属窗口已关闭。")
        top_version_name_level.destroy()

    top_version_name_level.protocol("WM_DELETE_WINDOW", saved_and_quit_toplevel)

    return None


def show_versions_list():
    versions_info = get_versions_list(download_api_choose.get())
    latest_info = versions_info["latest"]
    releases_info = list(versions_info["release"])
    snapshots_info = list(versions_info["snapshot"])
    latest_release.set(latest_info["release"] + " 正式版")
    latest_snapshot.set(latest_info["snapshot"] + " 快照版")

    Label(versions_frame_latest, image=icon_release, compound="left", textvariable=latest_release,
          font=config.font_cof).grid(row=0, column=0)
    Label(versions_frame_latest, image=icon_snapshot, compound="left", textvariable=latest_snapshot,
          font=config.font_cof).grid(row=0, column=1)

    releases_listbox.delete(0, END)
    snapshots_listbox.delete(0, END)

    for i in releases_info:
        releases_listbox.insert("end", i["id"])
    for i in snapshots_info:
        snapshots_listbox.insert("end", i["id"])


def download_version_result_loader():
    T = threading.Thread(target=download_version_result, name="Download_loader", daemon=True,
                         args=(search_a_version_entry.get(), download_api_choose.get()))

    logger.Logger.debug("准备弹出新窗口。")
    T.start()
    logger.Logger.debug("附属窗口已经展示。")


def get_choice_from_listbox(listbox: Listbox):
    if not listbox.curselection():
        showwarning("未选择版本", "您至少需要选择一个版本，才能显示版本信息呀！", icon_baby_husk_targeting)
    else:
        return listbox.get(listbox.curselection())


# 启动窗口
style = Style(theme='fcltheme')
window = style.master
window.resizable(False, False)
window.title(config.window_title)
window.geometry("1005x530")

# 告诉操作系统使用程序自身的dpi适配
ctypes.windll.shcore.SetProcessDpiAwareness(1)
# 获取屏幕的缩放因子
ScaleFactor = ctypes.windll.shcore.GetScaleFactorForDevice(0)
# 设置程序缩放
window.tk.call('tk', 'scaling', ScaleFactor / 75)

# tk可变参量
player_name__ = StringVar()
game_version__ = StringVar()
java_version__ = StringVar()
max_memory__ = StringVar()
min_memory__ = StringVar()
player_uuid__ = StringVar()
bottom_progress = IntVar()
download_api__ = StringVar()
latest_release = StringVar()
latest_snapshot = StringVar()

# 获取版本信息和Java信息
game_version_tuple = main_func.get_list_tuple(0, local_files.game_versions_list(0))
java_version_tuple = main_func.get_list_tuple(0, local_files.java_versions_list(0), config.tk_prefix_java)

# ——————————————————————————↓窗口组件↓—————————————————————————— #
# 启动器相关
pagetab_main = Notebook()
pagetab_main.pack()
pagetab_1 = Frame(pagetab_main)
pagetab_1.pack()
pagetab_main.add(pagetab_1, text=config.tk_lang_title_pagetab_1)

# 启动游戏
launching_frame = Labelframe(pagetab_1, text=config.tk_lang_title_launching, style="success")
launching_frame.grid(row=0, column=0, sticky="n" + "s")

player_name_entry = Entry(launching_frame, textvariable=player_name__, font=config.font_cof)  # 玩家名
player_name_entry.grid(row=0, column=1, sticky="w" + "e")
Label(launching_frame, text=config.tk_lang_player_name_entry, font=config.font_cof).grid(row=0, column=0)

game_version_choose = Combobox(launching_frame, style="primary", textvariable=game_version__,
                               font=config.font_cof)  # 选择游戏版本
game_version_choose['values'] = game_version_tuple
game_version_choose.grid(row=1, column=0)

java_version_choose = Combobox(launching_frame, style="success", textvariable=java_version__,
                               font=config.font_cof)  # 选择Java版本
java_version_choose['values'] = java_version_tuple
java_version_choose.grid(row=1, column=1)

max_memory_entry = Entry(launching_frame, textvariable=max_memory__, font=config.font_cof)  # 最大内存
max_memory_entry.grid(row=2, column=1)
Label(launching_frame, text=config.tk_lang_max_memory_entry, font=config.font_cof).grid(row=2, column=0)

min_memory_entry = Entry(launching_frame, textvariable=min_memory__, font=config.font_cof)  # 最小内存
min_memory_entry.grid(row=3, column=1)
Label(launching_frame, text=config.tk_lang_min_memory_entry, font=config.font_cof).grid(row=3, column=0)
Label(launching_frame, text=config.tk_lang_memory_tip, font=config.font_cof).grid(row=2, column=2, rowspan=2)

launch_button = Button(launching_frame, text=config.tk_lang_launch_button, style="primary",
                       command=lambda: get_launch())  # 启动按钮
launch_button.grid(row=0, column=2, rowspan=2, columnspan=2)

# 高级设置-UUID
more_info_frame = Labelframe(pagetab_1, text=config.tk_lang_more_info, style="primary")
more_info_frame.grid(row=0, column=1)

Label(more_info_frame, text=config.tk_lang_player_uuid_entry, font=config.font_cof).grid(row=0, column=0)  # 输入UUID的提示
player_uuid_entry = Entry(more_info_frame, textvariable=player_uuid__, font=config.font_cof, style="info",
                          width=36)  # 获取UUID
player_uuid_entry.grid(row=0, column=1)
Label(more_info_frame, text=config.tk_lang_player_uuid_tips, font=config.font_cof).grid(row=1, column=0,
                                                                                        columnspan=2)  # 对UUID的简单介绍

# tk可变参量#2
normal_setting = local_files.local_setting_list(0)
FCLversion = normal_setting[0] + "-" + normal_setting[1]
player_name__.set(dict(normal_setting[2])["player_name"])
game_version__.set(dict(normal_setting[2])["game"])
java_version__.set(dict(normal_setting[2])["java"])
max_memory__.set(dict(normal_setting[2])["max_memory"])
min_memory__.set(dict(normal_setting[2])["min_memory"])
player_uuid__.set(dict(normal_setting[2])["player_uuid"])
version_feature_topic = StringVar()
version_feature_introduction = StringVar()

# 展示区域
background_frame = Labelframe(pagetab_1, text=config.tk_lang_title_background, style="success")
background_frame.grid(row=1, column=0, columnspan=2, sticky="w" + "e")
picture = Image.open(fp=bit_version_picture())
picture = picture.resize((360, 200))
feature_picture = ImageTk.PhotoImage(picture)
feature_label = Label(background_frame, image=feature_picture)
feature_label.grid(row=0, column=0, rowspan=4)
Label(background_frame, textvariable=version_feature_topic, font=("微软雅黑", 12)).grid(row=0, column=1)  # 版本特色介绍
Label(background_frame, textvariable=version_feature_introduction, width=70, wraplength=620, font=config.font_cof) \
    .grid(row=1, column=1,
          columnspan=3)  # 版本特色介绍
Button(background_frame, style="primary",command=lambda: check_whole_version(game_version_choose.get(), mode=download_api_choose.get()),
       text=config.tk_lang_download_version_lib_and_ass_button).grid(row=0, column=2)
Button(background_frame, style="info", text=config.tk_lang_open_version_wiki_pag, command=lambda: get_version_update_info(game_version_choose.get(),
       search_new_version(game_version_choose.get(), download_api_choose.get())[2])).grid(row=0, column=3)

# tk可变参量#3
version_temp = ".".join(func.get_version_info(0, game_version_choose.get())[0].split(".")[0:2])
temp_1, temp_2 = local_files.read_features(0, version_temp)
version_feature_topic.set("更新主题   " + temp_1)
version_feature_introduction.set("      " + temp_2)

# 底部进度条
bottom_frame = Labelframe(pagetab_1, text=config.tk_lang_title_bottom, style="warning")
bottom_frame.grid(row=999, column=0, columnspan=999, sticky="w" + "e")

bottom_progressbar = Progressbar(master=bottom_frame, variable=bottom_progress,
                                 style="warning", maximum=config.max_threading_count)  # 进度条本体
bottom_progressbar.pack(side="top", fill="x")
Label(bottom_frame, text=config.tk_lang_bottom_progressbar_tip, font=config.font_cof).pack()

# 第二页
pagetab_2 = Frame(pagetab_main)
pagetab_2.pack()
pagetab_main.add(pagetab_2, text=config.tk_lang_title_pagetab_2)

# tk可变参量#4
download_api__.set(dict(normal_setting[2])["download_api"])
version_num__ = StringVar()
version_json_url__ = StringVar()
version_type__ = StringVar()
version_update_date__ = StringVar()
version_release_date__ = StringVar()

# 查询/安装指定版本
search_a_version_frame = Labelframe(pagetab_2, text=config.tk_lang_title_search_a_version, style="info")
search_a_version_frame.pack()
search_a_version_entry = Entry(search_a_version_frame, style="info")
search_a_version_entry.grid(row=0, column=0)
Label(search_a_version_frame, text=config.tk_lang_search_a_version_tip, font=config.font_cof).grid(row=1, column=0)
Button(search_a_version_frame, text=config.tk_lang_search_button, style="info",
       command=lambda: search_version_result()).grid(row=0, column=1)
Label(search_a_version_frame, textvariable=version_num__, font=config.font_cof).grid(row=0, column=2)
Label(search_a_version_frame, textvariable=version_type__, font=config.font_cof).grid(row=0, column=3)
Label(search_a_version_frame, textvariable=version_update_date__, font=config.font_cof).grid(row=0, column=4)
Label(search_a_version_frame, textvariable=version_release_date__, font=config.font_cof).grid(row=0, column=5)
Label(search_a_version_frame, textvariable=version_json_url__, font=config.font_cof).grid(row=1, column=2, columnspan=4)
Button(search_a_version_frame, text=config.tk_lang_search_download_button,
       command=lambda: download_version_result_loader()).grid(row=1, column=1)

# 供下载的版本列表
versions_list_frame = Labelframe(pagetab_2, text=config.tk_lang_title_versions_list_frame, style="info")
versions_list_frame.pack()
versions_frame_latest = Labelframe(versions_list_frame, text=config.tk_lang_latest_versions_tip, style="primary")
versions_frame_latest.grid(row=0, column=0, columnspan=2)

versions_releases_list = Labelframe(versions_list_frame, text=config.tk_lang_releases_list_tip, style="warning")
versions_releases_list.grid(row=1, column=0, rowspan=2)

versions_snapshots_list = Labelframe(versions_list_frame, text=config.tk_lang_snapshots_list_tip, style="danger")
versions_snapshots_list.grid(row=1, column=1, rowspan=2)

releases_listbox = Listbox(versions_releases_list)
releases_listbox.pack()
snapshots_listbox = Listbox(versions_snapshots_list)
snapshots_listbox.pack()
Button(versions_releases_list, text=config.tk_lang_version_info_button, style="warning",
       command=lambda: get_version_update_info(get_choice_from_listbox(releases_listbox), "正式版")).pack()
Button(versions_snapshots_list, text=config.tk_lang_version_info_button, style="danger",
       command=lambda: get_version_update_info(get_choice_from_listbox(snapshots_listbox), "快照版")).pack()

Button(versions_list_frame, text=config.tk_lang_get_versions_list_button, command=lambda: show_versions_list()) \
    .grid(row=0, column=2)

Label(versions_list_frame, text=config.tk_lang_versions_list_tip).grid(row=1, column=2)

# 第三页
pagetab_3 = Frame(pagetab_main)
pagetab_3.pack()
pagetab_main.add(pagetab_3, text=config.tk_lang_title_pagetab_3)

# 第四页
pagetab_4 = Frame(pagetab_main)
pagetab_4.pack()
pagetab_main.add(pagetab_4, text=config.tk_lang_title_pagetab_4)

# 全局下载源
download_api_frame = Labelframe(pagetab_4, text=config.tk_lang_title_download_api, style="primary")
download_api_frame.pack()
download_api_choose = Combobox(download_api_frame, style="primary", textvariable=download_api__)
download_api_choose["value"] = ("Mojang", "BMCLAPI", "MCBBS")
download_api_choose.grid(row=0, column=0)
Label(download_api_frame, text=config.tk_lang_download_api_tip, font=config.font_cof).grid(row=0, column=1, rowspan=2)
BMCLAPI_help_frame = Labelframe(download_api_frame, text=config.tk_lang_BMCLAPI_help_frame, style="warning")
BMCLAPI_help_frame.grid(row=0, column=2, rowspan=2)
Label(BMCLAPI_help_frame, text=config.tk_lang_BMCLAPI_help_tip, font=config.font_cof).pack()
Label(download_api_frame, text=config.tk_lang_download_api_tip_2, font=config.font_cof).grid(row=1, column=0)

# ——————————————————————————↑窗口组件↑—————————————————————————— #

# 播放背景音乐
background_music()
show_versions_list()
bottom_progressbar_()


# 调试 关闭窗口时保存信息至game_versions.json
def saved_and_quit():
    logger.Logger.info("检测到退出事件，开始保存玩家配置。")
    player_name_ = player_name_entry.get()
    player_uuid_ = player_uuid_entry.get()
    max_memory_ = max_memory_entry.get()
    min_memory_ = min_memory_entry.get()
    java_ = java_version_choose.get()
    game_ = game_version_choose.get()
    download_api_ = download_api_choose.get()
    saved_info_package = (player_name_, player_uuid_, max_memory_, min_memory_, java_, game_, download_api_)
    local_files.saved_player_info(0, saved_info_package)
    logger.Logger.info("即将退出，感谢您的使用。")
    window.destroy()
    return None


window.protocol("WM_DELETE_WINDOW", saved_and_quit)
# 维持窗口
get_print_thread_count()
window.mainloop()
